-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
67 lines (59 loc) · 1.79 KB
/
Solution.java
File metadata and controls
67 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
class Stack {
private static final int MAX = 100;
private int[] stack = new int[MAX];
private int top = -1;
public void push(int value) {
if (top == MAX - 1) {
System.out.println("Stack overflow!");
} else {
stack[++top] = value;
System.out.println(value + " pushed into the stack.");
}
}
public void pop() {
if (top == -1) {
System.out.println("Stack underflow!");
} else {
System.out.println(stack[top--] + " popped from the stack.");
}
}
public void peek() {
if (top == -1) {
System.out.println("Stack is empty.");
} else {
System.out.println("Top element is: " + stack[top]);
}
}
}
public class StackDemo {
public static void main(String[] args) {
Stack stack = new Stack();
Scanner scanner = new Scanner(System.in);
int choice, value;
do {
System.out.println("\n1. Push\n2. Pop\n3. Peek\n4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
value = scanner.nextInt();
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 4);
scanner.close();
}
}